package com.arron.algorithm.leetcodetop100.链表;


import com.arron.algorithm.ListNode;


import java.util.PriorityQueue;

/**
 *
 *  合并 K 个升序链表
 *
 */
public class 合并K个升序链表 {

    public ListNode mergeKLists(ListNode[] lists) {

        PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(lists.length,(a,b)-> a.val-b.val);

        ListNode res  = new ListNode(-1);

        ListNode p1 = res;

        for (ListNode head : lists) {
            if (head!=null){
                priorityQueue.add(head);
            }
        }

        while (!priorityQueue.isEmpty()){

            ListNode node = priorityQueue.poll();

            p1.next = node;

            if (node.next!=null){
                priorityQueue.add(node.next);
            }

            p1 = p1.next;
        }

        return  res.next;

    }



}
